from pydantic import BaseModel, Field, model_validator


class StockItem(BaseModel):
    date: str = Field(..., description='日期')
    name: str = Field(..., alias='f14', description="股票名称")
    symbol: str = Field(..., alias='f12', description="股票代码")
    price: float = Field(..., alias='f2', description="最新价")
    market_type: int = Field(..., alias='f13', description="市场类型")
    highest_price: float = Field(..., alias='f15', description="最高价")
    lowest_price: float = Field(..., alias='f16', description="最低价")
    open_price: float = Field(..., alias='f17', description='今开')
    change_rate: float = Field(..., alias='f3', description="涨跌幅")
    change_amount: float = Field(..., alias='f4', description="涨跌额")
    volume: float = Field(..., alias='f5', description="成交量")
    trade_amount: float = Field(..., alias='f6', description="成交额")
    diff_rate: float = Field(..., alias='f7', description="振幅")
    change_hand_rate: float = Field(..., alias='f8', description="换手率")
    speed: float = Field(0, alias='f22', description='涨速', exclude=True)
    volume_rate: float = Field(0, alias='f10', description='量比', exclude=True)
    market_value: float = Field(0, alias='f20', description='总市值', exclude=True)
    l_market_value: float = Field(0, alias='f21', description='流通市值', exclude=True)

    # yesterday_price: float = Field(..., alias='f18', description='昨收', exclude=True)

    class Config:
        populate_by_name = True


class StockItemV2(StockItem):
    m5: float = Field(default=0.0, description="5日均价")
    m10: float = Field(default=0.0, description="10日均价")
    m20: float = Field(default=0.0, description="20日均价")
    m30: float = Field(default=0.0, description="30日均价")


class ChoiceParams(BaseModel):
    min_l_market_value: float | None
    max_l_market_value: float | None
    min_change_rate: float | None
    max_change_rate: float | None
    volume_rate: float | None
    min_change_hand_rate: float | None
    max_change_hand_rate: float | None
    min_vr: float | None
    max_vr: float | None
    min_up_line: float | None
    min_change_rate_20: float | None
    max_change_rate_20: float | None


class ZTItem(BaseModel):
    symbol: str = Field(..., alias='c', description="股票代码")
    name: str = Field(..., alias='n', description="股票名称")
    price: float = Field(..., alias='p', description="价格")
    market_type: int = Field(..., alias='m', description="市场类型")
    change_rate: float = Field(..., alias='zdp', description="涨跌幅")
    trade_amount: float = Field(..., alias='amount', description="成交额")
    l_market_value: float = Field(0, alias='ltsz', description='流通市值')
    market_value: float = Field(0, alias='tshare', description='总市值')
    change_hand_rate: float = Field(..., alias='hs', description="换手率")
    hybk: str = Field(..., alias='hybk', description="行业板块")
    lbc: float = Field(..., alias='lbc', description="连板数")
    fbt: str = Field(..., alias='fbt', description="首次封板时间")
    lbt: str = Field(..., alias='lbt', description="最后封板时间")
    fund: float = Field(..., alias='fund', description="封板资金")
    zbc: int = Field(..., alias='zbc', description="炸板次数")
    days: int = Field(..., alias='days', description="天")
    ct: int = Field(..., alias='ct', description="板数")

    diff_amount: float = Field(0, description="成交差")
    diff_fund: float = Field(0, description="封板差")
    cancel_fund: float = Field(0, description="撤单封板金额")
    new_fund: float = Field(0, description="新增封板金额")

    class Config:
        populate_by_name = True

    @model_validator(mode='before')
    def validate_date(cls, values):
        def f(s: str):
            res = s
            if len(s) == 5:
                res = "0" + res
            return "{}:{}:{}".format(res[:2], res[2:4], res[4:])

        if values.get("p"):
            values["fbt"] = f(str(values["fbt"]))
            values["lbt"] = f(str(values["lbt"]))
            values["p"] = round(values['p'] / 1000, 2)
            values['zdp'] = round(values['zdp'], 2)
        return values
